package edu.ccut.saturn.core.impl;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import edu.ccut.saturn.core.IPropertiesService;
/**
 * 
 * @author Tony Yan
 *
 */
public class SaturnPropertiesService implements IPropertiesService {

	private static IPropertiesService instance;
	
	static {
		instance = new SaturnPropertiesService();
	}
	
	private Map<String, Object> properties = new HashMap<String, Object>();
	
	private SaturnPropertiesService() {
	}
	
	public static IPropertiesService getInstance() {
		return instance;
	}

	@SuppressWarnings("unchecked")
	public void addProperties(Properties properties) {
		Enumeration keys = properties.propertyNames();
		
		synchronized (this.properties) {
			while (keys.hasMoreElements()) {
				String key = (String) keys.nextElement();
				String value = properties.getProperty(key);
				this.setProperty(key, value);
			}
		}
	}

	public synchronized String getProperty(String name) {
		return (String)this.properties.get(name);
	}

	@SuppressWarnings("unchecked")
	public synchronized Iterator getPropertyNames() {
		return this.properties.keySet().iterator();
	}

	public synchronized void setProperty(String name, String value) {
		this.properties.put(name, value);
	}
	

	public Object getPropertyObject(String name) {
		return this.properties.get(name);
	}

	public void setPropertyObject(String name, Object value) {
		this.properties.put(name, value);
	}
}
